package com.zip.example.controller;

import com.google.common.base.Splitter;
import lombok.extern.slf4j.Slf4j;
import net.lingala.zip4j.core.ZipFile;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author zgq
 * @date 2020/10/28 9:40
 */
@Slf4j
@RestController
public class UnZipController {

    public static void main1(String[] args) throws Exception {
        String zipPath = "E:\\张国庆\\ZipExample\\zip\\777888.zip";
        String savePath = "E:\\张国庆\\ZipExample\\unzip";
        File file = new File(savePath);
        // 判断是否是目录
        boolean directory = file.isDirectory();
        if (!directory) {
            System.out.println("保存的路径必须是目录路径");
            return;
        }
        // 没有目录则创建
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
        }
        //开始解压
        ZipFile zipFile = new ZipFile(new File(zipPath));
        zipFile.extractAll(savePath);
    }

    public static void main2(String[] args) {
        String filePath = "E:\\张国庆\\ZipExample\\unzip\\20201028\\1.jpg";
        String savePath = "E:\\张国庆\\ZipExample\\upload\\20201028";
        InputStream is = null;
        OutputStream os = null;
        try {
            File file = new File(filePath);
            String name = file.getName();
            String extension = FilenameUtils.getExtension(name);
            is = new FileInputStream(filePath);
            byte[] bytes = new byte[1024];
            int len = 0;
            os = new FileOutputStream(savePath + ".jpg");
            while ((len = is.read(bytes)) != -1) {
                os.write(bytes, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 根据文件路径删除下面所有东西
    public static void main3(String[] args) {
//        String dirPath = "E:\\upload\\unzip\\temp\\20201028";
        String dirPath = "E:\\upload\\unzip\\temp\\20201028";
        deleteFile(new File(dirPath));
        String zipPath = "E:\\张国庆\\ZipExample\\zip\\777888.zip";
    }

    public static Boolean deleteFile(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files.length != 0) {
                for (File file1 : files) {
                    if (file1.isDirectory()) {
                        deleteFile(file1);
                    } else {
                        boolean delete = file1.delete();
                    }

                }
                new File(file.getPath() + File.separator).delete();
            } else {
                new File(file.getPath() + File.separator).delete();
            }
        } else {
            boolean delete = file.delete();
        }
        return true;
    }


    public static void main(String[] args) {
        String zipPath = "E:\\张国庆\\ZipExample\\zip\\777888.zip";
        File file = new File(zipPath);
        System.out.println(FilenameUtils.getExtension(file.getName()));
        zipPath = "a,f.g";
        List<String> strings = Splitter.on(",").splitToList(zipPath).stream().map(s -> "123" + s).collect(Collectors.toList());
        System.out.println(String.join(",", strings));
    }
}
